package compiler

import (
	"tvl.fyi/tvix/eval/internal/index"
	"tvl.fyi/tvix/eval/internal/op"
	p "tvl.fyi/tvix/eval/parser"
	"tvl.fyi/tvix/eval/value"
)

func (c *compiler) compileString(slot index.Local, n *p.Node) {
	if len(n.Nodes) == 0 {
		c.pushConstant(slot, value.String(""))
		return
	}

	if len(n.Nodes) == 1 && n.Nodes[0].Type == p.TextNode {
		c.compileNode(slot, n.Nodes[0])
		return
	}

	c.unit().beginScope() // track string fragments (interpolation might have >1)

	for _, elem := range n.Nodes {
		c.compileNode(c.unit().declarePhantom(), elem)
	}

	c.unit().chunk.PushOpWithUint(op.Interpolate, uint64(len(n.Nodes)))
	c.unit().endScope()
}
